package com.example.woke;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.adapter.Mybanklistbadter;
import com.woke.data.Datas_bankedcard;
import com.woke.data.Datas_moprepay;
import com.woke.data.Datas_nocardback;
import com.woke.data.Datas_yeeprepay;
import com.woke.method.Intentnet;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class NocardpayActivity extends Activity implements OnClickListener {

	private TextView mTok;
	//	private ArrayList<String> cardinfo;
	private EditText mEname, mEcode, mEdayear,mEdayday, mEphone, mEsfz, mEthree;
	private String transDate, transTime;
	private boolean istype1 = false;// falseΪ���
	private String refMsg;
	//	private ArrayList<Data_bindcarlist> datalist = new ArrayList<Data_bindcarlist>();
	private ArrayList<Datas_bankedcard> datalsit;
	private TextView mTcarcode, mTcartype, mTcarthree, mTmoney;
	private LinearLayout mLinearLayout;
	private MyApp application;
	private RelativeLayout mMore;
	private TextView mTcode;
	private CheckBox mBox;
	private RelativeLayout mBar;

	private Datas_nocardback cardinfo;
	// ����Ϊ֧���Ĳ���
	private String cardType = "";// ������
	private String orderId = "";// ����id
	private float transAmount = 0;// ���׽��
	private String cardByNo = "";// ����
	private String expireDate = "";// ��Ч����
	private String expireday = "";// ��Ч����
	private String cvv = "";// λ��
	private String cardByName = "";// ����
	private String cerNumber = "";// ���֤
	private String mobile = "";// �ֻ��
	private String userid = "";// �û�id
	//	private String bindid = "";//�󶨺�
	private String ip ="";//�ֻ�����ip

	private String ksPayOrderId;// ī�����صĶ�����
	private LayoutInflater inflater;
	private int jtime = 60;

	private String kspayorderid2;//ħ�����صĶ�����

	private Intent intent;

	private Runnable runnableRef = new Runnable() {
		public void run() {

        	if(jtime == 0){
        		Message msg = new Message();
        		msg.what = 911 ;
        		mHandler.sendMessage(msg);
        		mHandler.removeCallbacks(runnableRef);
        	}else{
				jtime = jtime - 1;
        		Message msg = new Message();
        		msg.what = 109 ;
        		mHandler.sendMessage(msg);
			}

        	mHandler.postDelayed(runnableRef, 1000);
		}
	};

    Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if(msg.what == 911){
				mTcode.setText("点击发送验证码");
				mHandler.removeCallbacks(runnableRef);
				jtime = 60;
				mTcode.setClickable(true);
				mTcode.setBackgroundResource(R.drawable.rect_lightred);
			}else if(msg.what == 109){
				mTcode.setText("("+jtime+")秒后重新发送");
				mTcode.setBackgroundResource(R.drawable.rect_lightgray);
				mTcode.setClickable(false);
			}else if(msg.what == 1){
//				mBar.startAnimation(
//						AnimationUtils.loadAnimation(RegisterActivity.this, R.anim.btn_rotate));
				mBar.setVisibility(View.VISIBLE);
			}else if(msg.what == 2){
//				mBar.clearAnimation();
				mBar.setVisibility(View.GONE);
			}
		}
	};
	private String mWay;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_nocardpay);

		intent = getIntent();
		intview();

	}

	private void intview() {
		cardinfo = (Datas_nocardback) intent.getSerializableExtra("cardlist");
		String cardNo=cardinfo.getCardno ();
		datalsit = (ArrayList<Datas_bankedcard>) intent.getSerializableExtra("datalist");
//		stringExtra = intent.getStringExtra("title");
		mWay = intent.getStringExtra ("way");
		inflater = getLayoutInflater();
		application = (MyApp) getApplication();
		ip = Intentnet.getPsdnIp();
		Log.e("IP地址", "ip:"+ip);
		mTcarcode = (TextView) findViewById(R.id.avnocarpay_text_cardcode);
		mTcartype = (TextView) findViewById(R.id.avnocarpay_text_cardtype);
		mTcarthree = (TextView) findViewById(R.id.avnocarpay_text_lasttrhee);
		findViewById(R.id.avnocard_image_back).setOnClickListener(this);
		mLinearLayout = (LinearLayout) findViewById(R.id.avnocar_when_xyk);
		mMore = (RelativeLayout) findViewById(R.id.avnocard_pay_morebinkcrad);
		mMore.setOnClickListener(this);
		mTmoney = (TextView) findViewById(R.id.avnocard_pay_money);// ���׽��
		mTok = (TextView) findViewById(R.id.avnocard_text_okpay);
		mTok.setOnClickListener(this);
		mBar = (RelativeLayout) findViewById(R.id.avnocarpay_frame_1);
		mTcode = (TextView) findViewById(R.id.avnocarpay_text_code);
		mTcode.setOnClickListener(this);
		mEname = (EditText) findViewById(R.id.avnocarpay_edit_name);
		mEcode = (EditText) findViewById(R.id.avnocarpay_edit_code);
		mEdayear = (EditText) findViewById(R.id.avnocarpay_edit_year);//���ÿ�ʱ��
		mEdayday = (EditText) findViewById(R.id.avnocarpay_edit_day);//���ÿ�ʱ��
		mEphone = (EditText) findViewById(R.id.avnocarpay_edit_phone);
		mEsfz = (EditText) findViewById(R.id.avnocarpay_edit_sfz);
		mEthree = (EditText) findViewById(R.id.avnocarpay_edit_three);
		findViewById(R.id.avnocarpay_xieyi).setOnClickListener(this);
		mBox = (CheckBox) findViewById(R.id.avnocarpay_checkbox);
		mBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					mTok.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.rect_lightred));
					mTok.setClickable(true);
				} else {
					mTok.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.rect_lightgray));
					mTok.setClickable(false);
				}
			}
		});
		textsettext();
		mEcode.addTextChangedListener(watcher);
	}

	private TextWatcher watcher = new TextWatcher() {
	    @Override
	    public void onTextChanged(CharSequence s, int start, int before, int count) {
	    }
	    @Override
	    public void beforeTextChanged(CharSequence s, int start, int count,
	            int after) {
	    }

	    @Override
	    public void afterTextChanged(Editable s) {
	    	String trim = ""+mEcode.getText().toString().trim();
	    	if(trim.equals("")){
	    		mTok.setBackgroundDrawable(getResources().getDrawable(R.drawable.rect_lightgray));
	    	}else{
	    		mTok.setBackgroundDrawable(getResources().getDrawable(R.drawable.rect_lightred));
	    	}
	    }
	};
//	private String stringExtra;
	private void textsettext(){
		// ����Ϊ��ʾ���ݹ�������Ϣ
		//������Ϣ˳��������ơ����š������͡��������֤���ֻ�š����׽������š��׻�ħ���ױ��󿨺�
		mTmoney.setText("￥"+cardinfo.getMoney());
		orderId = cardinfo.getId();
		transAmount = cardinfo.getMoney();
//		bindid = cardinfo.get(9);

				mTcarcode.setText(cardinfo.getBank());
				mTcarthree.setText("尾号"+cardinfo.getCardno().subSequence(
						cardinfo.getCardno().length() - 4, cardinfo.getCardno().length()));
				if (cardinfo.getCardtype().equals("1")) {
					mTcartype.setText("储蓄卡");
					mLinearLayout.setVisibility(View.GONE);
		} else {
					mLinearLayout.setVisibility(View.VISIBLE);
					mTcartype.setText("信用卡");
			istype1 = true;// ��ʱΪ���ÿ�
		}

		mEname.setText(cardinfo.getName());
		mEsfz.setText(cardinfo.getSfz());
		mEphone.setText(cardinfo.getPhone());
		mEthree.setText(cardinfo.getCvv());
		if(null != cardinfo.getValidthru() && !cardinfo.getValidthru().equals("")){
			mEdayear.setText(cardinfo.getValidthru().substring(0, 2));
			mEdayday.setText(cardinfo.getValidthru().substring(2, cardinfo.getValidthru().length()));
		}
		cardType = cardinfo.getCardtype();
		cardByNo = cardinfo.getCardno();
		mobile = cardinfo.getPhone();

	}



	public class PopupWindows extends PopupWindow implements OnItemClickListener {

		public PopupWindows(Context mContext) {

			View view = View
					.inflate(mContext, R.layout.item_adater_banklist, null);
			view.startAnimation(AnimationUtils.loadAnimation(mContext,
					R.anim.fade_ins));
			LinearLayout ll_popup = (LinearLayout) view
					.findViewById(R.id.lsduoie);
			ll_popup.startAnimation(AnimationUtils.loadAnimation(mContext,
					R.anim.push_bottom_in_2));

			setWidth(LayoutParams.FILL_PARENT);
			setHeight(LayoutParams.FILL_PARENT);
			setBackgroundDrawable(new BitmapDrawable());
			setFocusable(true);
			setOutsideTouchable(true);
			setContentView(view);
			showAtLocation(view, Gravity.BOTTOM, 0, 0);
			update();
			ImageView mView1 = (ImageView) view.findViewById(R.id.item_banklist_back);
			ListView mListView = (ListView) view.findViewById(R.id.item_banklist_listview);
			mListView.setOnItemClickListener(this);
			Mybanklistbadter adapter = new Mybanklistbadter(inflater,datalsit);
			mListView.setAdapter(adapter);

			mView1.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					dismiss();
				}
			});
		}

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			//������Ϣ˳��������ơ����š������͡��������֤���ֻ�š����׽������š��׻�ħ���ױ��󿨺�
			Datas_bankedcard list = datalsit.get(arg2);
//			cardinfo.clear();
			cardinfo.setBank(list.getBrname());
			cardinfo.setCardno(list.getCardno());
			cardinfo.setCardtype(list.getType());
			cardinfo.setName(list.getName());
			cardinfo.setSfz(list.getIdcard());
			cardinfo.setPhone(list.getPhone());
			cardinfo.setCvv(list.getCode());
			cardinfo.setValidthru(list.getValidthru());
			textsettext();
			dismiss();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.avnocard_text_okpay:

			cardByName = mEname.getText().toString().trim();
			cerNumber = mEsfz.getText().toString().trim();
			mobile = mEphone.getText().toString().trim();
			cvv = "" + mEthree.getText().toString().trim();
			expireDate = "" + mEdayear.getText().toString().trim();
			expireday = "" + mEdayday.getText().toString().trim();
			String phocode = "" + mEcode.getText().toString().trim();
				if (null == cardByName || cardByName.equals("")) {
					Toast.makeText(NocardpayActivity.this, "请输入姓名",
							Toast.LENGTH_SHORT).show();
				} else if (null == cerNumber || cerNumber.equals("")) {
					Toast.makeText(NocardpayActivity.this, "请输入身份证",
							Toast.LENGTH_SHORT).show();
				} else if (null == mobile || mobile.equals("")) {
					Toast.makeText(NocardpayActivity.this, "请输入预留手机号",
							Toast.LENGTH_SHORT).show();
				} else if (istype1 == true && cvv.equals("")) {
					Toast.makeText(NocardpayActivity.this, "请输入信用卡背面安全码3位数",
							Toast.LENGTH_SHORT).show();
				} else if (istype1 == true && expireDate.equals("")) {
					Toast.makeText(NocardpayActivity.this, "请输入信用卡有效期月",
							Toast.LENGTH_SHORT).show();
				} else if (istype1 == true && expireday.equals("")) {
					Toast.makeText(NocardpayActivity.this, "请输入信用卡有效期年份",
							Toast.LENGTH_SHORT).show();
				}else if (phocode.equals("")) {
					Toast.makeText(NocardpayActivity.this, "请输入短信验证码",
							Toast.LENGTH_SHORT).show();
				} else {
//					if(stringExtra.equals("yeepay")){
//						yeepay(phocode);
//					}else{
//					paymopay(phocode);
//					}
//					mopaypay(phocode);//�ױ���ī��ͳһ�ӿ�
				}
				break;

			case R.id.avnocard_pay_morebinkcrad:
			if(null != datalsit && datalsit.size() > 0){
				new PopupWindows(NocardpayActivity.this);
			}else{
				}
				break;

			case R.id.avnocard_image_back:
			finish();
				break;

		case R.id.avnocarpay_xieyi:
			Toast.makeText(NocardpayActivity.this, "这里需要修改",Toast.LENGTH_SHORT).show();
		break;

		case R.id.avnocarpay_text_code:
			cardByName = mEname.getText().toString().trim();
			cerNumber = mEsfz.getText().toString().trim();
			mobile = mEphone.getText().toString().trim();
			cvv = "" + mEthree.getText().toString().trim();
			expireDate = "" + mEdayear.getText().toString().trim();
			expireday = "" + mEdayday.getText().toString().trim();

				if (null == cardByName || cardByName.equals("")) {
					Toast.makeText(NocardpayActivity.this, "请输入姓名",Toast.LENGTH_SHORT).show();
				} else if (null == cerNumber || cerNumber.equals("")) {
					Toast.makeText(NocardpayActivity.this, "请输入身份证",
							Toast.LENGTH_SHORT).show();
				} else if (null == mobile || mobile.equals("")) {
					Toast.makeText(NocardpayActivity.this, "请输入预留手机号",
							Toast.LENGTH_SHORT).show();
				} else if (istype1 == true && null == cvv) {
					Toast.makeText(NocardpayActivity.this, "请输入信用卡背后3位数",
							Toast.LENGTH_SHORT).show();
				} else if (istype1 == true && null == expireDate) {
					Toast.makeText(NocardpayActivity.this, "请输入信用卡有效期月",
							Toast.LENGTH_SHORT).show();
				} else if (istype1 == true && expireday.equals("")) {
					Toast.makeText(NocardpayActivity.this, "请输入信用卡有效期年份",
							Toast.LENGTH_SHORT).show();
				}else {
					// ����ȥ��ȡ������֤��
//					getdatacode(mobile);//�в��Դ������֤
//					if(stringExtra.equals("yeepay")){
//						getyeepaycode(mobile);
//					}else{
//					getmoopaycode(mobile);
//					}
				}

			break;

			default:
				break;
		}
	}

	private void yeepay(String phocode) {
		if(application.getDatas_load ()==null)return;
		AsyncHttpClient ahc = new AsyncHttpClient();
		PersistentCookieStore myCookieStore = new PersistentCookieStore(
				NocardpayActivity.this);
		ahc.setCookieStore(myCookieStore);
		RequestParams params = new RequestParams();
		//������Ϣ˳��������ơ����š������͡��������֤���ֻ�š����׽������š��׻�ħ���ױ��󿨺�
		params.put("g", "api");
		params.put("m", "yeePay");
		params.put("a", "confirmPayment");
		params.put("orderid", orderId);// ���׽��
		params.put("code", phocode);// ��֤��
		params.put("userid", application.getDatas_load().getId());
		params.put("money", transAmount);// ���׽��
		params.put("phone", mobile);// �ֻ��

		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler() {

		@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				// TODO Auto-generated method stub
				super.onSuccess(statusCode, headers, response);
				Log.e("支付结果", "response"+response);
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					if (flag.equals("unknow")) {
						Toast.makeText(NocardpayActivity.this, "" + info,Toast.LENGTH_SHORT).show();
					} else if (flag.equals("success")) {
						Toast.makeText(NocardpayActivity.this, ""+info,Toast.LENGTH_SHORT).show();
						mTok.setClickable(false);
						mTok.setBackgroundDrawable(getResources().getDrawable(R.drawable.rect_lightgray));
						intent.setClass(NocardpayActivity.this, ZcaipayokActivity.class);
						intent.putExtra("user_nicename", "user_nicename");
						startActivity(intent);
						finish();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
//g=api&a=confirmPayment&code=710953&m=yeeYay&phone=18650034010&userid=33&orderid=2016051011562475&money=1.0
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				mBar.setVisibility(View.GONE);
				Toast.makeText(NocardpayActivity.this, "数据失败！",Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mBar.setVisibility(View.VISIBLE);
		}

			@Override
			public void onFinish() {
				super.onFinish();
				mBar.setVisibility(View.GONE);
			}
		});
	}

	private void paymopay(String phocode) {
		if(application.getDatas_load ()==null)return;
		AsyncHttpClient ahc = new AsyncHttpClient();
		PersistentCookieStore myCookieStore = new PersistentCookieStore(
				NocardpayActivity.this);
		ahc.setCookieStore(myCookieStore);
		RequestParams params = new RequestParams();
		//������Ϣ˳��������ơ����š������͡��������֤���ֻ�š����׽������š��׻�ħ���ױ��󿨺�
		params.put("g", "api");
		params.put("m", "mopay");
		params.put("a", "pay");
//		params.put("requestId", orderId);// ���׽��
		if(istype1 == true){
			params.put("cardType", "00");// ���ÿ�
		}else{
			params.put("cardType", "01");// ���
		}
		params.put("transAmount", transAmount);// ���׽��
		params.put("cardByNo", cardByNo);// ����
		params.put("expireDate", expireday+expireDate);// ��Ч��
		params.put("cvv", cvv);
		params.put("cardByName", cardByName);// ����
		params.put("cerNumber", cerNumber);// ֤������
		params.put("mobile", mobile);// �ֻ��
		params.put("yzm", phocode);// ��֤��
		params.put("ksPayOrderId", kspayorderid2);// ip
//		Log.e("cardByName", "cardByName" + cardByName);
		params.put("userid", application.getDatas_load().getId());
		Log.e("魔宝知道", "" + params);
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				// TODO Auto-generated method stub
				super.onSuccess(statusCode, headers, response);
				Log.e("支付结果", "response"+response);
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					if (flag.equals("unknow")) {
						JSONObject jsonObject = response.getJSONObject("data");
						JSONObject jsonObject2 = jsonObject.getJSONObject("data");
						String refmsg = jsonObject2.getString("refmsg");
						Toast.makeText(NocardpayActivity.this,info+","+refmsg,Toast.LENGTH_SHORT).show();
					} else if (flag.equals("success")) {
						Toast.makeText(NocardpayActivity.this, "支付成功"+info,Toast.LENGTH_SHORT).show();
						mTok.setClickable(false);
						mTok.setBackgroundDrawable(getResources().getDrawable(R.drawable.rect_lightgray));
						intent.setClass(NocardpayActivity.this, ZcaipayokActivity.class);
						intent.putExtra("user_nicename", "user_nicename");
						startActivity(intent);
						finish();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				mBar.setVisibility(View.GONE);
				Toast.makeText(NocardpayActivity.this, "数据失败！",Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mBar.setVisibility(View.VISIBLE);
			}

			@Override
			public void onFinish() {
				super.onFinish();
				mBar.setVisibility(View.GONE);
			}
		});
	}

	private void getmoopaycode(String mobile2) {
		if(application.getDatas_load ()==null)return;
		AsyncHttpClient ahc = new AsyncHttpClient();
		PersistentCookieStore myCookieStore = new PersistentCookieStore(NocardpayActivity.this);
		ahc.setCookieStore(myCookieStore);
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "mopay");
		params.put("a", "prePay");
		if(istype1 == true){
			params.put("cardType", "00");// ���ÿ�
		}else{
			params.put("cardType", "01");// ���
		}
		params.put("orderId", orderId);
		params.put("transAmount", transAmount);
		params.put("cardByNo", cardByNo);
		params.put("expireDate",expireday+expireDate);
		params.put("cvv", cvv);
		params.put("cardByName", cardByName);
		params.put("cerNumber", cerNumber);
		params.put("mobile", mobile2);
		params.put("userid", application.getDatas_load().getId());
//		Log.e("^^params", Contants.BASEURL+params.toString());
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("^^response", ""+response);
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					if(flag.equals("success")){
						JSONObject jsonObject = response.getJSONObject("data");
						String string = jsonObject.getString("data");
						Datas_moprepay parseObject = com.alibaba.fastjson.JSONObject.parseObject(string, Datas_moprepay.class);
						kspayorderid2 = parseObject.getKspayorderid();
						if(parseObject.getRefmsg().equals("预交易成功")){
							Toast.makeText(NocardpayActivity.this, "验证码已发送", Toast.LENGTH_SHORT).show();
							Thread thread = new Thread(runnableRef);
							thread.start();
						}else{
							Toast.makeText(NocardpayActivity.this, ""+parseObject.getRefmsg(), Toast.LENGTH_SHORT).show();
						}
					}else{
						JSONObject jsonObject = response.getJSONObject("data");
						JSONObject jsonObject2 = jsonObject.getJSONObject("data");
						String string = jsonObject2.getString("refmsg");
						Toast.makeText(NocardpayActivity.this, info+" "+string, Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
//				Log.e("response", "ʧ��"+responseString);
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mBar.setVisibility(View.VISIBLE);
			}

			@Override
			public void onFinish() {
				super.onFinish();
				mTcode.setClickable(true);
				mBar.setVisibility(View.GONE);
			}
		});
	}

	private void getyeepaycode(String mobile2) {
		if(application.getDatas_load ()==null)return;
		AsyncHttpClient ahc = new AsyncHttpClient();
		PersistentCookieStore myCookieStore = new PersistentCookieStore(NocardpayActivity.this);
		ahc.setCookieStore(myCookieStore);
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "yeePay");
		params.put("a", "pay");
		if(istype1 == true){
			params.put("cardType", "2");// ���ÿ�
		}else{
			params.put("cardType", "1");// ���
		}
		params.put("requestid", orderId);
		params.put("cardno", cardByNo);
		params.put("idcardno", cerNumber);
		params.put("username", cardByName);
		params.put("phone", mobile2);
		params.put("userip", ip);
		params.put("validthru", expireDate+expireday);
		params.put("cvv2", cvv);
		params.put("identityid", mobile2);
		params.put("terminalid", mobile2);
		params.put("amount", transAmount);
		params.put("userid", application.getDatas_load().getId());
		params.put("bindid", "");

		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("^^response", ""+response);
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					if(flag.equals("success")){
						JSONObject jsonObject = response.getJSONObject("data");
						String string = jsonObject.getString("data");
						Datas_yeeprepay parseObject = com.alibaba.fastjson.JSONObject.parseObject(string, Datas_yeeprepay.class);
						Toast.makeText(NocardpayActivity.this, "验证码已发送！", Toast.LENGTH_SHORT).show();
						Thread thread = new Thread(runnableRef);
		                thread.start();
					}else{
						Toast.makeText(NocardpayActivity.this, ""+info, Toast.LENGTH_SHORT).show();
//						{"time":1464835743,"info":"����Ա������������Ϣ","flag":"unknow"}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
//				Log.e("response", "ʧ��"+responseString);
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mBar.setVisibility(View.VISIBLE);
			}

			@Override
			public void onFinish() {
				super.onFinish();
				mTcode.setClickable(true);
				mBar.setVisibility(View.GONE);
			}
		});
	}

	private void mopaypay(String phocode) {
		if(application.getDatas_load ()==null)return;
		AsyncHttpClient ahc = new AsyncHttpClient();
		PersistentCookieStore myCookieStore = new PersistentCookieStore(
				NocardpayActivity.this);
		ahc.setCookieStore(myCookieStore);
		RequestParams params = new RequestParams();
		//������Ϣ˳��������ơ����š������͡��������֤���ֻ�š����׽������š��׻�ħ���ױ��󿨺�
		params.put("g", "api");
		params.put("m", "pay");
		params.put("a", "pay");
		params.put("requestId", orderId);// ���׽��
		if(istype1 == true){
			params.put("cardType", "2");// ���ÿ�
		}else{
			params.put("cardType", "1");// ���
		}
		params.put("cardNo", cardByNo);// ����
		params.put("cardPhone", mobile);// �ֻ��
		params.put("name", cardByName);// ����
		params.put("ip", ip);// ip
		params.put("idCardNo", cerNumber);// ֤������
		params.put("cardExpire", expireDate+expireday);// ��Ч��
		params.put("cvv2", cvv);
		params.put("transAmount", transAmount);// ���׽��
//		Log.e("cardByName", "cardByName" + cardByName);
		params.put("userId", application.getDatas_load().getId());
		params.put("code", phocode);// ��֤��
		Log.e("params", "" + params);
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				// TODO Auto-generated method stub
				super.onSuccess(statusCode, headers, response);
//				Log.e("֧�����", "response"+response);
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					if (flag.equals("unknow")) {
						Toast.makeText(NocardpayActivity.this, "" + info,Toast.LENGTH_SHORT).show();
					} else if (flag.equals("success")) {
						Toast.makeText(NocardpayActivity.this, "支付成功"+info,Toast.LENGTH_SHORT).show();
						mTok.setClickable(false);
						mTok.setBackgroundDrawable(getResources().getDrawable(R.drawable.rect_lightgray));
						intent.setClass(NocardpayActivity.this, ZcaipayokActivity.class);
						intent.putExtra("user_nicename", "user_nicename");
						startActivity(intent);
						finish();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				mBar.setVisibility(View.GONE);
				Toast.makeText(NocardpayActivity.this, "数据失败！",Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mBar.setVisibility(View.VISIBLE);
			}

			@Override
			public void onFinish() {
				super.onFinish();
				mBar.setVisibility(View.GONE);
			}
		});
	}

	private void getdatacode(String phone) {
		AsyncHttpClient ahc = new AsyncHttpClient();
		PersistentCookieStore myCookieStore = new PersistentCookieStore(NocardpayActivity.this);
		ahc.setCookieStore(myCookieStore);
		RequestParams params = new RequestParams();
//		params.put("g", "api");
//		params.put("m", "Public");
//		params.put("a", "sendCode");
//		params.put("username", phone);
		params.put("g", "api");
		params.put("m", "pay");
		params.put("a", "getPayCode");
		params.put("phone", phone);
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				try {
					String flag = response.getString("flag");
					if(flag.equals("success")){
						Toast.makeText(NocardpayActivity.this, "验证码已发送！", Toast.LENGTH_SHORT).show();
						Thread thread = new Thread(runnableRef);
		                thread.start();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				Log.e("response", "失败"+responseString);
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mBar.setVisibility(View.VISIBLE);
			}

			@Override
			public void onFinish() {
				super.onFinish();
				mTcode.setClickable(true);
				mBar.setVisibility(View.GONE);
			}
		});
	}
}
